﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Modele;
using System.ComponentModel;


namespace Guitar.CustomControls
{
    /// <summary>
    /// Logique d'interaction pour ComboboxNotes.xaml
    /// </summary>
    public partial class BtnNote : UserControl
    {


        public EventHandler OnValueChanged;

        private Note _note = new Note();

        public Note note
        {
            get { return _note; }
            set
            {
                _note = value;
                Update();
                if (OnValueChanged != null)
                    OnValueChanged(this, EventArgs.Empty);

            }
        }



        public List<Note> allNote = Note.NotesPossibles();




        public BtnNote()
        {
            
        }


        public BtnNote(Note not)
        {

            InitializeComponent();
            note = not;
            for (int i = 4; i <= 8; i++)
            {

                MenuItem tmp = new MenuItem();
                tmp.Header = "Octave";
                Label num = new Label();
                num.Foreground = Brushes.Black;
                num.FontWeight = FontWeights.Bold;
                num.Content = i;
                tmp.Icon = num;

                for (int j = 0 + (i * 17); j < (i * 17) + 17; j++)
                {
                    MenuItem tmp2 = new MenuItem();
                    string[] nameNote = allNote[j].ToString(App.option.LangueNotationMusicale);

                    Label notel = new Label();
                    Label alter = new Label();
                    alter.Foreground = Brushes.Black;
                    alter.FontWeight = FontWeights.Bold;
                    notel.Content = nameNote[0];

                    alter.Content = nameNote[1];

                    tmp2.Header = notel;
                    tmp2.Icon = alter;
                    tmp2.Tag = j;
                    tmp2.Click += new RoutedEventHandler(Click_MenuItem);

                    tmp.Items.Add(tmp2);

                }
                menu_note.Items.Add(tmp);



            }


        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            int cpt = 68;

            for (int i = 4; i <= 8; i++)
            {

                for (int k = 0; k < 17; k++)
                {

                    // On met a jour chaques éléments au  cas ou la langue change   
                    string[] nameNote = allNote[cpt].ToString(App.option.LangueNotationMusicale);


                    Label notel = new Label();
                    Label alter = new Label();
                    alter.Foreground = Brushes.Black;
                    alter.FontWeight = FontWeights.Bold;
                    notel.Content = nameNote[0];
                    alter.Content = nameNote[1];

                    ((MenuItem)((MenuItem)menu_note.Items[i-4]).Items[k]).Icon = alter;
                    ((MenuItem)((MenuItem)menu_note.Items[i-4]).Items[k]).Header = notel;
                    cpt++;
                    alter = null;
                    notel = null;


                }

            }

            menu_note.IsOpen = true;
        }

        private void Click_MenuItem(object sender, EventArgs e)
        {
            note = allNote[Convert.ToInt16(((MenuItem)sender).Tag)];

        }

        public void Update()
        {

            String[] nameNote = note.ToString(App.option.LangueNotationMusicale);
            lbl_note.Content = nameNote[0] + nameNote[1] + " " + nameNote[2];


        }

    }
}